package mysql

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"manuscript-service/appconfig"
)

var err error
var DB *gorm.DB

func Init() {
	mysqlCon := appconfig.Config.Mysql
	dsn := fmt.Sprintf("%v:%v@tcp(%v:%v)/%v?charset=utf8mb4&parseTime=True&loc=Local",
		mysqlCon.User,
		mysqlCon.Password,
		mysqlCon.Host,
		mysqlCon.Port,
		mysqlCon.Database)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	}
	fmt.Println("数据库连接成功")
	err = DB.AutoMigrate(&Goods{}, Class{})
	if err != nil {
		panic(err)
	}
	fmt.Println("迁移成功")
}
